package com.example.nacosprovider.service;

import com.example.nacosprovider.dao.UserMapper;
import com.example.nacosprovider.pojo.user;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.*;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.logging.Logger;

@Service("MyUserDetailsService")
@Slf4j
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    UserMapper userMapper;
    JavaUserService javaUserService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println(("用户名:" + s));
        List<user> userList = userMapper.getUserAll();
        for (user user : userList) {
            if (user.getName().equals(s)) {
                System.out.println("用户存在");
                List<GrantedAuthority> auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
                return new User(user.getName(),
                        new BCryptPasswordEncoder().encode(user.getPasswd()), auths);
            }
        }
        throw new UsernameNotFoundException("用户名不存在！");
    }
}

